var EXPORTED_SYMBOLS = ["HandyTabDataStore"];

var HandyTabDataStore = {
  initdone: false,
  file: null,
  marks: [],
  newMarks: [],
  dirty: false,

  init: function() {
    if (this.initdone) return;
    this.initdone = true;
    
    var f = Components.classes["@mozilla.org/file/directory_service;1"]
                      .getService(Components.interfaces.nsIProperties)
                      .get("ProfD", Components.interfaces.nsIFile);

    f.append("handytab");
    if (!f.exists() || !f.isDirectory()) {
      f.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0700);
    }
    f.append("marks.json");
    
    this.file = f;
  },
  
  getMarks: function(all) {
    this.marks = this.marks.concat(this.newMarks);
    var str = (all ? JSON.stringify(this.marks) : JSON.stringify(this.newMarks));
    this.newMarks = [];

    return str;
  },
  
  addMark: function(mark) {
    this.dirty = true;
    this.newMarks.push(mark);
  },
  
  removeMarks: function(items) {
    if (items == null) {
      if (this.marks.length == 0 && this.newMarks.length == 0) return;
      this.marks = [];
      this.newMarks = [];
      this.dirty = true;
      return;
    }
    
    if (items.length == 0) return;
    
    items = items.split(",");
    for (var i = 0; i < items.length; i++) {
      var index = parseInt(items[i]);
      this.marks.splice(index, 1);
      this.dirty = true;
    }
  },
  
  reset: function() {
    this.marks = [];
    this.newMarks = [];
    this.dirty = false;
    
    if (this.file.exists()) this.file.remove(false);
  },
  
  save: function(removeOnly) {
    if (!this.dirty) return;
    this.dirty = false;
    
    this.marks = this.marks.concat(this.newMarks);
    this.newMarks = [];
    
    var fstream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                            .createInstance(Components.interfaces.nsIFileOutputStream);
    var cstream = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
                            .createInstance(Components.interfaces.nsIConverterOutputStream);

    fstream.init(this.file, 0x02 | 0x08 | 0x20, 0600, 0); 
    cstream.init(fstream, "UTF-8", 0, 0);

    var savestr = JSON.stringify(this.marks);
    cstream.writeString(savestr);

    cstream.close();
    fstream.close();
  },
  
  load: function() {
    this.marks = [];
    this.dirty = false;

    if (!this.file.exists()) return;
    
    var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                            .createInstance(Components.interfaces.nsIFileInputStream);
    var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
                            .createInstance(Components.interfaces.nsIConverterInputStream);

    fstream.init(this.file, -1, 0, 0);
    cstream.init(fstream, "UTF-8", 0, 0);

    var loadstr = "";
    
    var data = {};
    cstream.readString(-1, data);
    loadstr = data.value;

    cstream.close();
    fstream.close();
    
    try {
      this.marks = JSON.parse(loadstr);
    } catch (e) {
      dump("handytab: failed to parse data file!\n");
      this.file.copyTo(null, "marks.json.failed.bak");
    }
  }
  
};
